package com.cbl.v.ssczs.view.activity;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Toast;

import com.cbl.v.ssczs.R;
import com.cbl.v.ssczs.mode.util.ActivityGroup;
import com.cbl.v.ssczs.view.dialog.CommonDialog;
import com.cbl.v.ssczs.view.fragment.BackHandlerHelper;
import com.cbl.v.ssczs.view.fragment.KJFragment1;
import com.cbl.v.ssczs.view.fragment.OtherFragment;
import com.cbl.v.ssczs.view.fragment.RMFragment1;
import com.cbl.v.ssczs.view.fragment.WFFragment;

import butterknife.OnClick;

public class LocalTabActivity extends BaseActivity {
    private Fragment[] fragments;
    private int index;
    private int currentTabIndex;
    private RMFragment1 homeFragment;
    private KJFragment1 kjFragment;
    private WFFragment wfFragment;
    private OtherFragment otherFragment;
    private CommonDialog dialog;

    @Override
    protected int getLayout() {
        return R.layout.activity_local_tab;
    }

    @Override
    public void initView() {
        hideTitleBar();
        dialog = new CommonDialog(getContext());
    }

    @Override
    public void initData() {
        kjFragment = new KJFragment1();
        otherFragment = new OtherFragment();
        wfFragment = new WFFragment();
        homeFragment = new RMFragment1();
        fragments = new Fragment[]{homeFragment, kjFragment, otherFragment, wfFragment};

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!homeFragment.isAdded()) {
            transaction.add(R.id.fragment_container, homeFragment);
        }
        if (!kjFragment.isAdded()) {
            transaction.add(R.id.fragment_container, kjFragment);
        }
        if (!otherFragment.isAdded()) {
            transaction.add(R.id.fragment_container, otherFragment);
        }
        if (!wfFragment.isAdded()) {
            transaction.add(R.id.fragment_container, wfFragment);
        }
        transaction.show(homeFragment).hide(kjFragment).hide(otherFragment).hide(wfFragment);
        transaction.commit();
    }

    @Override
    public void initListener() {

    }

    @OnClick({R.id.bt_kj, R.id.bt_other, R.id.bt_wf, R.id.bt_home})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_home:
                index = 0;
                //                setTitleNotBack("热门推荐");
                hideTitleBar();
                break;
            case R.id.bt_kj:
                index = 1;
                //                setTitleNotBack("天津时时彩开奖");
                hideTitleBar();
                break;
            case R.id.bt_other:
                index = 2;
                hideTitleBar();
                //                setTitleNotBack("汇总");
                break;
            case R.id.bt_wf:
                hideTitleBar();
                //                setTitleNotBack("玩法");
                index = 3;
                break;
        }
        if (currentTabIndex != index) {
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            trx.hide(fragments[currentTabIndex]);
            if (!fragments[index].isAdded()) {
                trx.add(R.id.fragment_container, fragments[index]);
            }
            trx.show(fragments[index]).hide(fragments[currentTabIndex]).commit();
        }
        currentTabIndex = index;
        super.onClick(view);
    }

    @Override
    public void dialogSure(String data) {
        ActivityGroup.finishAllActivity();
        super.dialogSure(data);
    }

    //    @Override
    //    public void onBackPressed() {
    //        if (!BackHandlerHelper.handleBackPress(this)) {
    //            dialog.setAlert("是否退出该应用？");
    //            dialog.setDialogClickListener(this);
    //            dialog.show();
    //            super.onBackPressed();
    //        }
    //    }
    private long lastBackPress;

    @Override
    public void onBackPressed() {
        if (!BackHandlerHelper.handleBackPress(this)) {
            if (System.currentTimeMillis() - lastBackPress < 1000) {
                super.onBackPressed();
            } else {
                lastBackPress = System.currentTimeMillis();
                Toast.makeText(LocalTabActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
            }
        }

    }
}
